package com.cxg.fairy.ec.main.personal.coupon;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.MultipleRecyclerAdapter;
import com.cxg.fairy.ui.recycler.MultipleViewHolder;
import com.diabin.latte.ec.R;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * Created by Administrator on 2018/3/8.
 */

public class AllCouponListAdapter extends MultipleRecyclerAdapter {

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .dontAnimate();

    Bitmap bp = null;

    public AllCouponListAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(CouponItemType.COUPON_ITEM,R.layout.item_coupon_list);
    }

    @Override
    protected void convert(final MultipleViewHolder holder, final MultipleItemEntity entity) {
        super.convert(holder, entity);
        switch (holder.getItemViewType()) {
            case CouponItemType.COUPON_ITEM:
                final String couponNo = entity.getField(CouponItemFields.COUPON_NO);
                final String couponName = entity.getField(CouponItemFields.COUPON_NAME);
                final String couponStatus = entity.getField(CouponItemFields.COUPON_STATUS);
                final String couponValidTime = entity.getField(CouponItemFields.COUPON_VALID_TIME);
                final String couponDesc = entity.getField(CouponItemFields.COUPON_DESC);

                final AppCompatTextView atvCouponNo = holder.getView(R.id.act_coupon_no);
                final AppCompatTextView actCouponName = holder.getView(R.id.act_coupon_name);
                final AppCompatImageView actCouponStatus = holder.getView(R.id.aci_coupon_img_status);
                final AppCompatTextView actCouponValidTime = holder.getView(R.id.act_coupon_valid_time);
                final AppCompatTextView actCouponDesc = holder.getView(R.id.act_coupon_desc);

                atvCouponNo.setText("NO."+couponNo);
                actCouponName.setText(couponName);

                if ("use1".equalsIgnoreCase(couponStatus)) {

                } else {
                    if ("status0".equalsIgnoreCase(couponStatus)||"use0".equalsIgnoreCase(couponStatus)) {
                        bp = getBitmapStream(R.mipmap.coupon_alreadyused); //已使用
                    } else if ("status10".equalsIgnoreCase(couponStatus)){
                        bp = getBitmapStream(R.mipmap.coupon_aboutdue);//即将过期
                    } else if ("status-1".equalsIgnoreCase(couponStatus)) {
                        bp = getBitmapStream(R.mipmap.coupon_alreadydue); //已过期
                    }
                    actCouponStatus.setBackground(new BitmapDrawable(mContext.getResources(),bp));
                }

                actCouponValidTime.setText(couponValidTime);
                actCouponDesc.setText(couponDesc);

                break;
            default:
                break;
        }
    }

    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
        return 30;
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }


    public Bitmap getBitmapStream(int id)
    {
        BitmapFactory.Options bmFactOpt = new BitmapFactory.Options();
        bmFactOpt.inPreferredConfig = Bitmap.Config.RGB_565;//表示16位位图 565代表对应三原色占的位数
        bmFactOpt.inInputShareable = true;
        bmFactOpt.inPurgeable = true;//设置图片可以被回收
        InputStream is = mContext.getResources().openRawResource(id);
        Bitmap bitmap = BitmapFactory.decodeStream(is, null, bmFactOpt);//比decodeResource()节省内存
        try
        {
            is.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return bitmap;
    }

}
